﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TradingApp.Helper;
using TradingAppDal;

namespace TradingApp
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        
        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateUI())
                {
                    if (tbPassword.Text.ToLower() == "cobra")
                    {
                        XmlHelper.SetTagValue(TradingAppSettings.USER_ATTRIBUTE_STR, tbUserName.Text.Trim()); ;
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Incorrect Password.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private bool ValidateUI()
        {
            StringBuilder sbError = new StringBuilder();
            if (String.IsNullOrEmpty(tbUserName.Text.Trim()))
            {
                sbError.Append(Environment.NewLine + " User Name cannot be empty.");
            }
            if (String.IsNullOrEmpty(tbPassword.Text.Trim()))
            {
                sbError.Append(Environment.NewLine + " Password cannot be empty.");
            }
            if (String.IsNullOrEmpty(tbRegistrationCode.Text.Trim()))
            {
                sbError.Append(Environment.NewLine + " Registration Code cannot be empty.");
            }

            if (sbError.Length == 0)
            {
                return true;
            }
            else
            {
                MessageBox.Show(sbError.ToString(), "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            LoadInitialData();
        }

        private void LoadInitialData()
        {
            try
            {
                tbUserName.Text = XmlHelper.GetStringTagValue(TradingAppSettings.USER_ATTRIBUTE_STR); 
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Login", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.OK)
            {
                Application.Exit();
            }
        }
    }
}
